package com.yunyao.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunyao.common.web.util.StringUtils;
import com.yunyao.dao.model.BamuSysDict;
import com.yunyao.dao.service.bamu.IBamuSysDictService;
import com.yunyao.common.dto.dict.DataDictionary;
import com.yunyao.framework.service.system.DataDictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 字典服务实现类
 * @author chenyejian
 * @date 2024-07-21
 */
@Slf4j
@Service
public class DataDictionaryServiceImpl implements DataDictionaryService {
    @Autowired
    private IBamuSysDictService dictService;

    @Override
    public List<DataDictionary> getDictionaryList(String parentCode) {
        /**
         * 查询code=parentCode并且is_deleted=0的并且parent_id不等于0的数据
         */
        LambdaQueryWrapper<BamuSysDict> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(parentCode)) {
            queryWrapper.eq(BamuSysDict::getCode, parentCode).eq(BamuSysDict::getIsDeleted, 0).ne(BamuSysDict::getParentId, 0);
            /**
             * dictList根据sort进行排序
             */
            queryWrapper.orderBy(true, true, BamuSysDict::getSort);
        }else {
            queryWrapper.eq(BamuSysDict::getIsDeleted, 0).ne(BamuSysDict::getParentId, 0);
        }
        List<BamuSysDict> dictList = dictService.list(queryWrapper);

        return dictList2DataDicList(dictList);
    }

    @Override
    public Map<String, List<DataDictionary>> getDictionaryListMap() {
        List<DataDictionary> dataDictionaryList = getDictionaryList(null);
        if (dataDictionaryList != null && dataDictionaryList.size() > 0) {
            return dataDictionaryList.stream().sorted(Comparator.comparing(DataDictionary::getSort)).collect(Collectors.groupingBy(DataDictionary::getGroupCode));
        }
        return null;
    }

    private List<DataDictionary> dictList2DataDicList(List<BamuSysDict> dictList) {
        try {
            /**
             * 遍历dictList，将dictList中的数据转换为DataDictionary对象
             */
            List<DataDictionary> dataDictionaryList = new ArrayList<>();
            dictList.forEach(dict -> {
                DataDictionary dataDictionary = new DataDictionary();
                dataDictionary.setGroupCode(dict.getCode());
                dataDictionary.setKey(dict.getDictKey());
                dataDictionary.setValue(dict.getDictValue());
                dataDictionary.setRemark(dict.getRemark());
                dataDictionary.setSort(dict.getSort());
                dataDictionary.setId(dict.getId());
                dataDictionary.setParentId(dict.getParentId());
                dataDictionaryList.add(dataDictionary);
            });
            return dataDictionaryList;
        }catch (Exception e) {
            log.error("数据字典转换失败", e);
            return null;
        }
    }
}
